#ifndef __URLMON_WATCHER__
#define __URLMON_WATCHER__

static mm::dllwp_item_t url_watcher_apis [] = 
{
	//{ "urlmon.dll", "URLDownloadToFileA", 5 * 4 },
	{ "urlmon.dll", "URLDownloadToFileW", 5 * 4 },
	//{ "urlmon.dll", "URLDownloadToCacheFileA", 6 * 4 },
	{ "urlmon.dll", "URLDownloadToCacheFileW", 6 * 4 },
	{ },
};

struct urlmon_watcher : mm::basic_watcher_t
{
	urlmon_watcher() : mm::basic_watcher_t(url_watcher_apis)
	{
	}
	//////////////////////////////////////////////////////////////////////////
	virtual long impl_after_call( size_t uid, hk::exec_context * pctx )
	{
		xv::xvalue_t _action;
		if( !_action.asObject() ) return NORMAL_LEAVE;

		//////////////////////////////////////////////////////////////////////////
		if( 0 )
		{
			const size_t frame_depth = 32;
			void * frames[frame_depth] = {};
			size_t depth = fetch_frames( pctx, frames, frame_depth );
			xvilize_frames( frames, depth, _action["context"]["frame"] );
		}
		//////////////////////////////////////////////////////////////////////////
		if( mm::is_api( uid, "URLDownloadToFileA" ) )
		{
			HRESULT hr = (HRESULT)pctx->retval();
			_action["action"] = "r3:url:download";
			_action["success"] = SUCCEEDED(hr);
			_action["payload"]["url"] = tcs::as_to_as(pctx->args[1]);
			_action["payload"]["local"] = tcs::as_to_as(pctx->args[2]);
			_action["payload"]["hr"] = hr;
		}
		else if( mm::is_api( uid, "URLDownloadToFileW" ) )
		{
			HRESULT hr = (HRESULT)pctx->retval();
			_action["action"] = "r3:url:download";
			_action["success"] = SUCCEEDED(hr);
			_action["payload"]["url"] = tcs::ws_to_as(pctx->args[1]);
			_action["payload"]["local"] = tcs::ws_to_as(pctx->args[2]);
			_action["payload"]["hr"] = hr;
		}
		else if( mm::is_api( uid, "URLDownloadToCacheFileA" ) )
		{
			HRESULT hr = (HRESULT)pctx->retval();
			_action["action"] = "r3:url:download";
			_action["success"] = SUCCEEDED(hr);
			_action["payload"]["url"] = (const char *)pctx->args[1];
			if( SUCCEEDED(hr) )
				_action["payload"]["local"] = (const char *)(pctx->args[2]);
			_action["payload"]["hr"] = hr;
		}
		else if( mm::is_api( uid, "URLDownloadToCacheFileW" ) )
		{
			HRESULT hr = (HRESULT)pctx->retval();
			_action["action"] = "r3:url:download";
			_action["success"] = SUCCEEDED(hr);
			_action["payload"]["url"] = tcs::ws_to_as(pctx->args[1]);
			if( SUCCEEDED(hr) )
				_action["payload"]["local"] = tcs::ws_to_as(pctx->args[2]);
			_action["payload"]["hr"] = hr;
		}
		else
		{
			return NORMAL_LEAVE;
		}
		throw_event( _action );
		return NORMAL_LEAVE;
	};

};


#endif